<template>

<el-form ref="queryFormRef" :inline="true" :model="queryForm" >
    <el-form-item style="width: 240px" label="菜单名称" prop="menuname" >
      <el-input v-model="queryForm.menuname" placeholder="menuame" />
    </el-form-item>
    <el-form-item  style="width: 240px" label="上级编号" prop="pid">
      <!-- <el-input v-model="queryForm.pid" placeholder="pid" clearable /> -->
      <el-select
        v-model="queryForm.pid"
        placeholder="请选择上级菜单"
      >
      <!-- 写死选项 不好
           动态数据 从数据库查 
      -->
        <el-option label="无" value="0" />
        <el-option v-for="option in optionList.optionData" :label="option.menuname" :value="option.mid" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="querySubmit">查询</el-button>
      <el-button  @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

<!-- table中经常要搭配数据插槽 template 做数据优化 -->
   <el-table :data="tableList.tableData" height="500" style="width: 100%">
      <el-table-column prop="mid" label="编号" />
      <el-table-column prop="menuname" label="名称" />
      <el-table-column prop="pid" label="上级菜单" >
         <template #default="scope">
            <el-tag v-if="scope.row.pid == 0" type="warning">{{scope.row.pid }}</el-tag>
            <el-tag v-else type="primary">{{scope.row.pid }}</el-tag>
         </template>
      </el-table-column>
      <el-table-column prop="pname" label="上级名称" >
         <template #default="scope">
            <el-tag v-if="scope.row.pid == 0" type="warning">{{scope.row.pname }}</el-tag>
            <el-tag v-else type="primary">{{scope.row.pname }}</el-tag>
         </template>
      </el-table-column>
      <el-table-column prop="show" label="是否启用" >
         <template #default="scope">
            <el-tag v-if="scope.row.show == 1" type="success">启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
         </template>
      </el-table-column>
      <el-table-column prop="url" label="访问地址" />
      <el-table-column prop="glyphicon" label="菜单图标" >
         <template #default="scope">
            <el-tag type="primary">
               <el-icon><component :is="scope.row.glyphicon"></component></el-icon>
            </el-tag> 
            <el-tag type="primary">{{scope.row.glyphicon }}</el-tag>

         </template>
      </el-table-column>
   </el-table>

   <el-pagination
      v-model:current-page="pageInfo.pageData.page"
      v-model:page-size="pageInfo.pageData.pageSize"
      :total="pageInfo.pageData.total"
      :page-sizes="[10, 20, 30]"
      layout="total, sizes, prev, pager, next, jumper"
      
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />


</template>

<script setup>
import {ref,reactive,onMounted} from 'vue'
import {get,post} from '@/myaxios'
import router from '@/router'
import {Edit,Histogram,SwitchButton} from '@element-plus/icons-vue'


//查询功能
//下拉列表数组
const optionList = reactive({optionData:[]})

//查询表单对象
const queryFormRef = ref()

//查询条件表单
const queryForm = reactive({
   username:'',
   pid:''
})


//分页组件对应数据
const pageInfo = reactive({pageData:{
   page:1,
   pageSize:10,
   total:33
}})
//table对应数据列表
const tableList = reactive({tableData:[]})
//切换页码
const handleCurrentChange = async (val)=>{
   console.log(val);
   //准备参数
   pageInfo.pageData.page = val;
   let params = pageInfo.pageData;
   let params2 = queryForm;
   let allParam = {...params,...params2};
   //调用查询函数
   queryData(allParam)
}
//切换每页显示记录数
const handleSizeChange = async (val)=>{
   console.log(val);
    //准备参数
   pageInfo.pageData.page = 1;
   pageInfo.pageData.pageSize = val;
   let params = pageInfo.pageData;
   let params2 = queryForm;
   let allParam = {...params,...params2};
    //调用查询函数
   queryData(allParam)

   
}
//查询按钮提交
const querySubmit = ()=>{
   //准备参数
   let params = queryForm;
   // //调用查询函数
   queryData(params)

// let param1 = {name:'jack'};
// let param2 = {age:18};
// //合并json对象
// let allParam = {...param1,...param2};
// console.log(allParam);


}
//重置查询
const resetQuery = ()=>{
   //清空表单
   queryFormRef.value.resetFields()
   //按默认条件查询数据
   queryData({})
}


//公共函数
//查询函数
const queryData = async (params)=>{
   let resp =  await get("queryUser",params);
  console.log(resp);
  //根据响应码做处理
  if(resp.data.returnCode ==  10000){
      tableList.tableData = resp.data.returnData;
      pageInfo.pageData = resp.data.pageInfo;
  }else if(resp.data.returnCode ==  10010){
      tableList.tableData = [];
      pageInfo.pageData = {page:1,pageSize:10,total:0};
  }
 
}
//菜单下拉列表
const queryOptions = async ()=>{
   let resp =  await get("queryUserMenuOptions",{});
   console.log(resp);
   optionList.optionData = resp.data.returnData
}


onMounted( async ()=>{
   queryData({})
   queryOptions()
})


</script>

<style scoped>

</style>